package com.qfhd.soft.cjzcore.download.limit;

import android.util.Log;

import com.qfhd.soft.cjzcore.Handle.CJHandelMessage;
import com.qfhd.soft.cjzcore.Handle.CJHandle;
import com.qfhd.soft.cjzcore.Handle.CJHanlderType;
import com.qfhd.soft.cjzcore.Handle.CJHanlderWhatType;
import com.qfhd.soft.cjzcore.download.CJDownloadInfo;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by zs
 * Date：2018年 09月 12日
 * Time：13:50
 * —————————————————————————————————————
 * About: 观察者
 * —————————————————————————————————————
 */
public class CJDownloadLimitObserver implements Observer<com.qfhd.soft.cjzcore.download.CJDownloadInfo> {

    public Disposable d;//可以用于取消注册的监听者
    public CJDownloadInfo CJDownloadInfo;

    @Override
    public void onSubscribe(Disposable d) {
        this.d = d;
    }

    @Override
    public void onNext(CJDownloadInfo value) {
        this.CJDownloadInfo = value;
        CJDownloadInfo.setDownloadStatus(CJDownloadInfo.DOWNLOAD);
//        EventBus.getDefault().post(CJDownloadInfo);
//        CJEvent.TaskTry.getEventtBus().post(new CJEventObject(CJEventObjectType.Download, CJDownloadInfo));
        CJHandle.getInstance().sendmessage(new CJHandelMessage(CJHanlderType.tryPlace, CJHanlderWhatType.Download,CJDownloadInfo));
    }

    @Override
    public void onError(Throwable e) {
        Log.d("My_Log","onError");
        if (CJDownloadLimitManager.getInstance().getDownloadUrl(CJDownloadInfo.getUrl())){
            CJDownloadLimitManager.getInstance().pauseDownload(CJDownloadInfo.getUrl());
            CJDownloadInfo.setDownloadStatus(CJDownloadInfo.DOWNLOAD_ERROR);
//            EventBus.getDefault().post(CJDownloadInfo);
//            CJEvent.TaskTry.getEventtBus().post(new CJEventObject(CJEventObjectType.Download, CJDownloadInfo));
//            CJHandle.getInstance().sendmessage(new CJHandelMessage(CJHanlderType.tryPlace, CJHanlderWhatType.Doanload));
            CJHandle.getInstance().sendmessage(new CJHandelMessage(CJHanlderType.tryPlace, CJHanlderWhatType.Download,CJDownloadInfo));
        }else{
            CJDownloadInfo.setDownloadStatus(CJDownloadInfo.DOWNLOAD_PAUSE);
//            EventBus.getDefault().post(CJDownloadInfo);
//            CJEvent.TaskTry.getEventtBus().post(new CJEventObject(CJEventObjectType.Download, CJDownloadInfo));
//            CJHandle.getInstance().sendmessage(new CJHandelMessage(CJHanlderType.tryPlace, CJHanlderWhatType.Doanload));
            CJHandle.getInstance().sendmessage(new CJHandelMessage(CJHanlderType.tryPlace, CJHanlderWhatType.Download,CJDownloadInfo));
        }

    }

    @Override
    public void onComplete() {
        Log.d("My_Log","onComplete");
        if (CJDownloadInfo != null){
            CJDownloadInfo.setDownloadStatus(CJDownloadInfo.DOWNLOAD_OVER);
//            EventBus.getDefault().post(CJDownloadInfo);
//            CJEvent.TaskTry.getEventtBus().post(new CJEventObject(CJEventObjectType.Download, CJDownloadInfo));
            CJHandle.getInstance().sendmessage(new CJHandelMessage(CJHanlderType.tryPlace, CJHanlderWhatType.Download,CJDownloadInfo));
        }
    }
}
